package co.yixiang.modules.shop.rest;

import co.yixiang.aop.log.Log;
import co.yixiang.modules.shop.domain.BywhShop;
import co.yixiang.modules.shop.service.BywhShopService;
import co.yixiang.modules.shop.service.dto.BywhShopQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
* @author lijunze
* @date 2020-05-10
*/
@Api(tags = "维修养护管理")
@RestController
@RequestMapping("/api/bywhShop")
public class BywhShopController {

    private final BywhShopService bywhShopService;

    public BywhShopController(BywhShopService bywhShopService) {
        this.bywhShopService = bywhShopService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('bywhShop:list')")
    public void download(HttpServletResponse response, BywhShopQueryCriteria criteria) throws IOException {
        bywhShopService.download(bywhShopService.queryAll(criteria), response);



    }

    @GetMapping
    @Log("查询维修养护")
    @ApiOperation("查询维修养护")
    @PreAuthorize("@el.check('bywhShop:list')")
    public ResponseEntity<Object> getBywhShops(BywhShopQueryCriteria criteria, Pageable pageable){

        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("code",200);
        resultMap.put("message",bywhShopService.queryAll(criteria,pageable));
        return ResponseEntity.ok(resultMap);
    }

    @PostMapping
    @Log("新增维修养护")
    @ApiOperation("新增维修养护")
    @PreAuthorize("@el.check('bywhShop:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody BywhShop resources){
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("code",200);
        resultMap.put("message",bywhShopService.create(resources));
        return ResponseEntity.ok(resultMap);
    }

    @PutMapping
    @Log("修改维修养护")
    @ApiOperation("修改维修养护")
    @PreAuthorize("@el.check('bywhShop:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody BywhShop resources){
        bywhShopService.update(resources);
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("code",200);
        return ResponseEntity.ok(resultMap);
    }

    @Log("删除维修养护")
    @ApiOperation("删除维修养护")
    @PreAuthorize("@el.check('bywhShop:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody List<Integer> ids) {
        bywhShopService.deleteAll(ids.toArray(new Integer[]{}));
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("code",200);
        return ResponseEntity.ok(resultMap);
    }
}